package com.jpl.demo.mobility.business.service.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import com.jpl.demo.mobility.business.enums.BusLineEnum;
import com.jpl.demo.mobility.business.enums.BusStopEnum;
import com.jpl.demo.mobility.business.model.BusStop;
import com.jpl.demo.mobility.business.service.BusService;
import com.jpl.demo.mobility.ui.util.BusStopHtmlPageParser;


public class BusServiceImpl implements BusService {

	private static final long serialVersionUID = -1308621227602770614L;
	private static BusService instance;

	
	private BusServiceImpl() {
		// Nothing to do in this singleton constructor
	}
	
	public BusStop getBusStopInformation(BusLineEnum line, BusStopEnum busStop) throws MalformedURLException,
			IOException {

		BusStop result = new BusStop(line.getLineNb(), line.name(), busStop.name());

		List<String> timings = BusStopHtmlPageParser.parse(line.getLineNb(), busStop.getDirection().name(),
				busStop.getTlCode());
		if (!timings.isEmpty()) {
			timings.remove(timings.size() - 1);
		}
		result.setNextTimes(timings);
		// remove last parasite element.
		return result;
	}

	public static final BusService getInstance() {
		if (instance == null) {
			instance = new BusServiceImpl();
		}
		return instance;

	}

}
